#include "EeeOCTray.h"
#include <QtDebug>
#include <QApplication>
#include <QMenu>
#include <QFile>
#include <QProcess>
#include <QTimer>

QTimer *statsTimer;
QTimer *fsbTimer;

/**
 *
 */
EeeOCTray::EeeOCTray() : QSystemTrayIcon()
{
	quitAction = new QAction(QIcon(":/quit.png"), tr("&Quit"), this);
	connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

	setContextMenu(new QMenu());

	readStats();

	desiredFsb = fsb.toInt();
	qDebug() << "desired:" << desiredFsb;

	updateMenu();

	statsTimer = new QTimer(this);
	connect(statsTimer, SIGNAL(timeout()), this, SLOT(readStats()));
	statsTimer->start(5*1000); // 5 seconds

	fsbTimer = new QTimer(this);
	connect(fsbTimer, SIGNAL(timeout()), this, SLOT(updateFsb()));
}

void EeeOCTray::fsbHigh()
{
	desiredFsb = 100;
	fsbTimer->start(0.5*1000);
}

void EeeOCTray::fsbMid()
{
	desiredFsb = 85;
	fsbTimer->start(0.5*1000);
}

void EeeOCTray::fsbLow()
{
	desiredFsb = 70;
	fsbTimer->start(0.5*1000);
}

void EeeOCTray::updateMenu()
{
	if(fsb == "100") {
		qDebug() << "FSB 100 Menu";
		fsbHighAction = new QAction(QIcon(":/ressources/active.png"), tr("&High"), this);
		fsbMidAction = new QAction(tr("&Medium"), this);
		fsbLowAction = new QAction(tr("&Low"), this);
	} else if(fsb == "85") {
		qDebug() << "FSB 85 Menu";
		fsbHighAction = new QAction(tr("&High"), this);
		fsbMidAction = new QAction(QIcon(":/ressources/active.png"), tr("&Medium"), this);
		fsbLowAction = new QAction(tr("&Low"), this);
	} else {
		qDebug() << "FSB Default Menu";
		fsbHighAction = new QAction(tr("&High"), this);
		fsbMidAction = new QAction(tr("&Medium"), this);
		fsbLowAction = new QAction(QIcon(":/ressources/active.png"), tr("&Low"), this);
	}
	connect(fsbHighAction, SIGNAL(triggered()), this, SLOT(fsbHigh()));
	connect(fsbMidAction, SIGNAL(triggered()), this, SLOT(fsbMid()));
	connect(fsbLowAction, SIGNAL(triggered()), this, SLOT(fsbLow()));

	QMenu *menu = contextMenu();
	menu->clear();
	menu->addAction(fsbHighAction);
	menu->addAction(fsbMidAction);
	menu->addAction(fsbLowAction);
	menu->addSeparator();
	menu->addAction(quitAction);
	setContextMenu(menu);
}

void EeeOCTray::setfsb(int newFsb)
{
	QTextStream in;
	QFile file;

	file.setFileName("/proc/eee/fsb");
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
		return;
	in.setDevice(&file);
	if (newFsb == 70)
		in << newFsb << " 24 0" << endl;
	else
		in << newFsb << " 24 1" << endl;
}

void EeeOCTray::updateFsb()
{

	readStats();
	if(fsb.toInt(NULL,10) == desiredFsb) {
		qDebug() << "FSB is at desired value\nStopping update timer";
		updateMenu();
		showMessage(tr("FSB has been changed"), "FSB is now at " + fsb + ".", QSystemTrayIcon::Information, 5000);
		fsbTimer->stop();
	} else if(fsb.toInt(NULL,10) > desiredFsb) {
		qDebug() << "Decreasing FSB by 1";
		setfsb(fsb.toInt(NULL, 10)-1);
	} else {
		qDebug() << "Increasing FSB by 1";
		setfsb(fsb.toInt(NULL, 10)+1);
	}
}

void EeeOCTray::readStats()
{
	QTextStream in;
	QFile file;
	QString tempLine;

	file.setFileName("/proc/eee/temperature");
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
		return;
	in.setDevice(&file);
	temperature = in.readLine();
	file.close();

	file.setFileName("/proc/eee/fsb");
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
		return;
	in.setDevice(&file);
	tempLine = in.readLine();
	fsb = tempLine.section(' ', 0, 0);
	voltage = tempLine.section(' ', 2, 2);
	file.close();

	file.setFileName("/proc/eee/fan_manual");
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
		return;
	in.setDevice(&file);
	mode = in.readLine();
	file.close();

	file.setFileName("/proc/eee/fan_rpm");
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
		return;
	in.setDevice(&file);
	rpm = in.readLine();
	file.close();

	file.setFileName("/proc/eee/fan_speed");
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
		return;
	in.setDevice(&file);
	speed = in.readLine();
	file.close();

	tempLine = "CPU FSB: " + fsb + " MHz";
	tempLine += "\nCPU Voltage: ";
	if(voltage == "0") {
		tempLine += "low";
	} else {
		tempLine += "high";
	}
	tempLine += "\nCPU Temperature: " + temperature + " " + '°' + "C";
	tempLine += "\n\nFan Mode: ";
	if(mode == "0") {
		tempLine += "automatic";
	} else {
		tempLine += "manual";
	}
	tempLine += "\nFan RPM: " + rpm;
	tempLine += " (" + speed + " %)";
	setToolTip(tempLine);

	tempLine = ":/ressources/" + temperature + ".png";
	setIcon(QIcon(tempLine));

}

// void QSystemTrayIcon::activated(QSystemTrayIcon::ActivationReason reason)
//  {
//         QProcess testProc;
// 	testProc.start("cat", QStringList() << "/proc/eee/fsb");
// 	testProc.waitForFinished();
// 	QByteArray result = testProc.readAll();
// 	showMessage(tr("Dies ist der Titel"), result, QSystemTrayIcon::Information, 5000);
//  }
//
// void QSystemTrayIcon::messageClicked ()
// {
// 	exit(0);
// }
